package com.wxp.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.models.constant.JafCoreConstant;
import com.modules.utils.BeanUtils;
import com.modules.utils.EncryptUtils;
import com.modules.utils.IDGenerator;
import com.wxp.dao.UserDao;
import com.wxp.entity.Role;
import com.wxp.entity.User;
import com.wxp.mapper.RoleMapper;
import com.wxp.service.UserService;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    
    @Autowired
    private RoleMapper roleMapper;

    public Page<User> findAll(Pageable pageable){

        return this.userDao.findAll(pageable);
    }
    public Page<User> findAll(Specification<User> spec,PageRequest pageRequest){
        return this.userDao.findAll(spec,pageRequest);
    }
    
    @Transactional(readOnly = true)
	public User get(String userId) {
    	
		return this.userDao.findOne(userId);
	}
    
    
    public User getByNameCode(String nameCode) {
    	return this.userDao.findFirstByNameCode(nameCode);
    }
    
    @Transactional(readOnly = true)
	public List<User> findAll() {
		return this.userDao.findAll();
	}
    
    @Transactional(readOnly = true)
   	public List<User> findAll(Specification<User> spec) {
   		return this.userDao.findAll(spec);
   	}
    
    public List<User> findAll(Specification<User> spec, Sort sort) {
		return this.userDao.findAll(spec,sort);
	}
    
	public String saveUser(User user) {
		String result = null;
		String primaryKey = user.getUserId();
		User existed = null;
		if(StringUtils.isNotBlank(primaryKey)) {
			existed = get(primaryKey);
		}
		if (existed == null) {
			user.setSalt(IDGenerator.generateSalt());
			user.setPassword(EncryptUtils.md5(user.getPassword(), EncryptUtils.base+user.getSalt()));
			this.userDao.save(user);
			result = JafCoreConstant.SAVE_SUCCESS;
		}else {
			if ((existed.getVersion() != null) && (existed.getVersion().intValue() != user.getVersion().intValue())){
				result = JafCoreConstant.OUT_OF_DATE;//已经被别人编辑更新过了
			}else {
				String[] properties = new String[] {"email","name","realName","telePhone","sex"
						
				};
				BeanUtils.copyAssignProperties(user, existed, properties);
				this.userDao.save(existed);
				result = JafCoreConstant.UPDATE_SUCCESS;
			}
		}
		return result;
	}
	
	public void deleteUser(User user) {
		this.userDao.delete(user);
		
	}

	
	public void deleteUser(String userId) {
		this.userDao.delete(userId);
	}
	
	public void saveUserRole(String userId,String data){
		ArrayList<Role> roleList = JSON.parseObject(data, new TypeReference<ArrayList<Role>>() {});
		userDao.deleteUserRole(userId);
		roleMapper.insertUserRole(userId,roleList);
	}
	
	public String  saveUserRoleProvider(Map map) {
		 @SuppressWarnings("unchecked")
		 List<Role> list = (List<Role>) map.get("list");  
		 String userId = String.valueOf(map.get("userId")); 
		 StringBuilder sb = new StringBuilder();  
		 sb.append("insert all "); 
		 
		 for (int i = 0; i < list.size(); i++) {  
			 sb.append(" into t_user_role (USER_ID_,ROLE_CODE_) values ('"+userId+"', '"+list.get(i).getRoleCode()+"') ");
	      }  
		 sb.append(" select 1 from dual");
		 return sb.toString();
	}
	
	
   


}
